import { request } from '../utils'

// 获取所有产品分类
const getCategories = cb => {
  request({
    url: 'api/Category/GetCategories',
    params: {}
  }).then(res => {
    if(res.data.Success){
      let list = res.data.Data[0].SubCategories
      list = list.map(item => {
        let newItem = {}
        newItem.Id = item.Id
        newItem.Name = item.Name
        newItem.Image = item.Image
        // 自分类过滤
        newItem.SubCategories = item.SubCategories.map(item => {
          let newItem = {}
          newItem.Id = item.Id
          newItem.Name = item.Name
          return newItem
        })
        return newItem
      })
      cb && cb(list)
    } else {
      console.log('获取分类错误...')
    }
  })
}

// 获取商家产品分类
const getShopCategories = cb => {
  request({
    url: "api/Category/GetShopCategories",
    params: {
      shopId: 1,
    },
  }).then((res) => {
    if (res.data.success) {
      let list = res.data.Category
      list = list.map((item) => {
        let newItem = {}
        newItem.Id = item.Id
        newItem.Name = item.Name
        newItem.Image = item.Image
        // 自分类过滤
        newItem.SubCategories = item.SubCategories.map((item) => {
          let newItem = {}
          newItem.Id = item.Id
          newItem.Name = item.Name
          return newItem
        })
        return newItem
      })
      cb && cb(list)
    } else {
      console.log("获取分类错误...")
    }
  })
}

// 获取推荐商品列表
const getAppIndex = cb => {
  request({
    url: "api/Home/GetAppIndex",
    params: {
      pageNo: 1,
      pageSize: 20,
    },
  }).then((res) => {
    if (res.data.Success) {
      let data = res.data.Data
      cb && cb(data)
    } else {
      console.log("获取首页数据错误...")
    }
  })
}

// 获取秒杀(限时购商品)列表
const getLimitTimeBuy = cb => {
  request({
    url: "api/LimitTimeBuy/GetLismitBuyList",
    params: {
      pageNo: 1,
      pageSize: 20,
    },
  }).then((res) => {
    if (res.data.Success) {
      let list = res.data.Data.List.reverse()
      let SeverTime = res.data.Data.SeverTime
      SeverTime = SeverTime * 1000
      cb && cb(list, SeverTime)
    } else {
      console.log("获取限时购商品失败...")
    }
  })
}

// 获取拼团商品列表
const getPinList = cb => {
  request({
    url: "api/FightGroup/GetActiveList",
    params: {
      userkey: localStorage.getItem('userKey'),
      page: 1,
    },
  }).then((res) => {
    if (res.data.Success) {
      let list = res.data.Data.GroupData
      cb && cb(list)
    } else {
      console.log("获取拼团商品失败...")
    }
  })
}

// 获取首页专题列表
const getTopicList = (cb) => {
  request({
    url: "api/Topic/GetTopicList",
    params: {
      pageNo: 1,
      pageSize: 20
    },
  }).then((res) => {
    if (res.data.success) {
      let list = res.data.Topic
      cb && cb(list)
    } else {
      console.log("获取专题列表失败...")
    }
  })
}

// 获取首页专题对应的产品列表
const getTopicDetail = (requestData, cb) => {
  request({
    url: "api/Topic/GetTopicDetail",
    params: requestData,
  }).then((res) => {
    if (res.data.success) {
      let topic = res.data.Topic
      cb && cb(topic)
    } else {
      console.log("获取专题详细失败...")
    }
  })
}

export {
  getCategories,
  getShopCategories,
  getAppIndex,
  getLimitTimeBuy,
  getPinList,
  getTopicList,
  getTopicDetail,
}